import cv2
import pylab
import numpy as np

lena = cv2.imread('train_image/lena.png', cv2.IMREAD_GRAYSCALE)
pylab.imshow(lena)
print(lena)

r, c = lena.shape[0], lena.shape[1]
print(lena.shape)
x = np.zeros((r, c, 8), dtype=np.uint8)
for i in range(8):
    x[:, :, i] = 2 ** i
r = np.zeros((r, c, 8), dtype=np.uint8)
for i in range(8):
    r[:, :, i] = cv2.bitwise_and(lena, x[:, :, i])
    # 将像素值大于0的都设为255，目的在于提高亮度，否在值过低就接近黑色了
    # mask = r[:, :, i] > 0
    # r[mask] = 255
    pylab.gray()
    pylab.subplot(3, 3, i + 1)
    pylab.imshow(r[:, :, i])
    pylab.axis('off')  # 去掉坐标轴
    pylab.title(str(i + 1))
pylab.show()

